//
//  CustomView.m
//  gdsgfd
//
//  Created by dllo on 15/11/19.
//  Copyright (c) 2015年 NL. All rights reserved.
//

#import "CustomView.h"

#define OPENCENTERX 150.0
#define DIVIDWIDTH 70.0 //OPENCENTERX 对应确认是否打开或关闭的分界线。

@implementation CustomView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

-(id)initWithView:(UIView *)contentview parentView:(UIView *)parentview{
    
    self = [super initWithFrame:CGRectMake(0, 0, contentview.frame.size.width, contentview.frame.size.height)];
    if (self) {
        self.contenView = contentview;
        self.parentView = parentview;
        
        [self addSubview:contentview];
//        滑动
        UIPanGestureRecognizer *panGestureRecognize = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
        [self addGestureRecognizer:panGestureRecognize];
//        单击
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
        [self addGestureRecognizer:tapGestureRecognizer];
        openPointCenter = CGPointMake(self.parentView.center.x + OPENCENTERX, self.parentView.center.y);
        NSLog(@"openPointCenter x: %f, openPointCenter y: %f", openPointCenter.x, openPointCenter.y);
    }
    return self;
    
}
//滑动方法
-(void)handlePan:(UIPanGestureRecognizer *)recognizer{
//    手势的指定视图，主页面
    CGPoint translation = [recognizer translationInView:self.parentView];
    
    float x = self.center.x + translation.x;
    NSLog(@"translation x:%f", translation.x);
    NSLog(@"parentView%f",self.frame.size.width);
    
    if (x < self.parentView.center.x) {
        x = self.parentView.center.x;
    }
    self.center = CGPointMake(x, openPointCenter.y);
//    滑动手势结束的时候
    if (recognizer.state == UIGestureRecognizerStateEnded) {
//        设置动画持续时间、等待时间，动画曲线
        [UIView animateWithDuration:0.75 delay:0.01 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
            if (x > openPointCenter.x - DIVIDWIDTH) {
                self.center = openPointCenter;
            }else{
                self.center = CGPointMake(openPointCenter.x - OPENCENTERX, openPointCenter.y);
            }
        }completion:^(BOOL isFinish){
         }];
    }
    [recognizer setTranslation:CGPointZero  inView:self.parentView];
}
//单击方法
-(void)handleTap:(UITapGestureRecognizer *)recognizer{
    [UIView animateWithDuration:0.75 delay:0.01 options: UIViewAnimationOptionTransitionCurlUp animations:^(void){
        self.center = CGPointMake(openPointCenter.x - OPENCENTERX, openPointCenter.y);
    }completion:nil];
}

@end
